/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gw2lol;

import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Alex
 */
public class SelectEvent extends javax.swing.JFrame {

    
    
    /**
     * Creates new form SelectServer
     */
    public SelectEvent(String worldId) {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SelectEvent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SelectEvent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SelectEvent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SelectEvent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }






JOptionPane.showMessageDialog(null,    "This might take a while. Please wait");
        System.out.println(worldId);
        theEvents = Gw2ApiInterface.loadEventNames();
        theMaps = Gw2ApiInterface.loadMapNames();
        theText = Gw2ApiInterface.load("events.json?world_id=" + worldId);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable2.setAutoCreateRowSorter(true);
        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            method(),
            TableHeads()
        ));
        jTable2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable2MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTable2);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1059, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable2MouseClicked

        rowSel = jTable2.getSelectedRow();

    }//GEN-LAST:event_jTable2MouseClicked

    public String searchEventNames(String ID){
        
        for (int i = 0; i < theEvents.size();i++)
        {
            if (ID.equals(theEvents.get(i).get(0).getVal()))
            {
                System.out.println(theEvents.get(i).get(1).getVal());
                return theEvents.get(i).get(1).getVal();
            }
        }
        return "Event Name Not Found";

    }
    
        public String searchMapNames(String ID){
        
        for (int i = 0; i < theMaps.size();i++)
        {
            if (ID.equals(theMaps.get(i).get(0).getVal()))
            {
                System.out.println(theMaps.get(i).get(1).getVal());
                return theMaps.get(i).get(1).getVal();
            }
        }
        return "Map Name Not Found";

    }
    
    public String[][] method() {
        
        String[][] theTable = new String[theText.size()][theText.get(0).size()-1];

        for (int i = 0; i < theText.size(); i++) {
                    int l = 0;
            for (int j = 0; j < theText.get(i).size(); j++) {
                if (j == 2) {
                    theTable[i][l] = searchEventNames(theText.get(i).get(j).getVal());
                    l++;
                }
                else if (j == 1) {
                    theTable[i][l] = searchMapNames(theText.get(i).get(j).getVal());
                    l++;
                }
                else if (j == 0) {
                }
                else{
                    theTable[i][l] = theText.get(i).get(j).getVal();
                    l++;
                }
            }
        }

        return theTable;
    }

    public String[] TableHeads() {
        String[] theTable = new String[theText.get(0).size()-1];
        int l = 0;
        for (int j = 0; j < theText.get(0).size(); j++) {
            if (j == 2) {
                theTable[j-1] = "Name of event";
            }
            else if (j == 1) {
                theTable[j-1] = "Name of map";
            }
            else if (j == 0) {
            }
            else{
            theTable[j-1] = theText.get(0).get(j).getType();
            }
        }


        return theTable;
    }
    
    public ArrayList<ArrayList<DataSet>> theEvents;
    public ArrayList<ArrayList<DataSet>> theMaps;
    public ArrayList<ArrayList<DataSet>> theText;
    int rowSel = -1;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable2;
    // End of variables declaration//GEN-END:variables
}
